package org.NooLab.glue.subscription;

import java.io.Serializable;
import java.util.Arrays;


/**
 * 
 * For controlling approximate matching. <br/>
 * It defines some measures and criteria to decide whether a particular 
 * transaction passing through the MessageBoard should be relayed to the
 * subscriber or not 
 * 
 *
 */
public class Context implements ContextIntf, Serializable{
 
	private static final long serialVersionUID = 4175458367756424525L;
	
	// =================================

	// object references ..............

	
	
	// constants ......................
	/**  items, texts       */
	transient public final static int _ITEMS   = 1 ; 
	
	/**  missing items, thinning item group  length, distances between elements   */
	transient public final static int _MIXES   = 3 ;  
	
	/**  weights, thresholds       */
	transient public final static int _WEIGHTS = 5 ;  
	
	/**  mandatory, normal, facultative, competing, exclusive: (mutual) exclusive subsets on items  */
	transient public final static int _SETS    = 7 ;   

	/**  rules of combination, organizational stuff  */
	transient public final static int _RULES   = 11 ;  

	/**  address in a particular associative region  */
	transient public final static int _AADDR   = 12 ;  
	
	// .................
	// descriptive detail parameter for sets 
	transient public final static int _SET_MANDATORY   = 1 ; // any missing item from this set denies the match
	transient public final static int _SET_NORMAL      = 2 ; // ...
	transient public final static int _SET_FACULTATIVE = 3 ; // does not count on thinning
	transient public final static int _SET_COMPETING   = 4 ; // if this is more similar, the match is denied
	transient public final static int _SET_EXCLUDING   = 5 ; // any item from this set denies the match

	// .................
	

	
	// main variables / properties ....

	String label = "";
	String[] itemSet = new String[0] ;
	 
	double missingItems; 
	double enlargement;
	double lengthRatio;
	double insertionLength ;
	
	double minimalMatch;
	double[] weights;
	 
	String[] mandatorySet ;
	String[] normalSet = new String[0] ; 
	String[] facultativeSet = new String[0] ;
	String[] competingSet = new String[0] ;
	String[] excludingSet = new String[0] ;
	
	boolean contextComplete = false;
	
	/** will be used as soon as there is more than 0 entries */
	String[] featureSet = new String[0] ;
	double[] itemValues = new double[0];
	
	// volatile variables .............
	transient int cdim=0;
	
	
	// helper objects .................
	 


	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
	public Context(){
		cdim = 0;
	}
	// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

	
	private void checkForCompleteness(){
		contextComplete = cdim>=3 ;
	}
	
	
	/**
	 * 
	 * @param label
	 * @param itemset
	 */
	public void addParameters( String label, String[] itemset){
		
		cdim = cdim + 1;
		
		try{
		
			this.label = label;
			itemSet = Arrays.copyOf( itemset,itemset.length ) ;
			
			checkForCompleteness();
		}catch(Exception e){
		}
		
	}
	
	/**
	 * 
	 *  missing items, thinning item group , length, distances between elements  _MIXES
	 *  
	 
	 * @param missingItems
	 * @param enlargement
	 * @param lengthratio
	 * @param insLength
	 */
	public void addParameters( double missingitems, double enlargement, double lengthratio, double insLength ){
		 
		
		try{
		
			cdim = cdim + 1;
			
			missingItems = missingitems ; 
			this.enlargement = enlargement ;
			lengthRatio = lengthratio ;
			insertionLength = insLength  ;
			
			checkForCompleteness();
		}catch(Exception e){
		}

	}



	
	/**
	 * 
	 *  
	 * 
	 */
	public void addParameters( double minMatch, double[] weigths ){
	 
		
		try{
			
			cdim = cdim + 1;
			
			minimalMatch = minMatch;
			this.weights = Arrays.copyOf( weigths,weigths.length ) ;
			
			checkForCompleteness();
		}catch(Exception e){
		}

	}
	
	
	/**
	 * 
	 * 
	 * @param cdim = _SETS
	 * @param setType
	 * @param items
	 */
	public void addParameters( int setType, String[] items  ){

		
		try{
			if (setType== _SET_MANDATORY){
				mandatorySet = Arrays.copyOf( items,items.length ) ;
			}
			if (setType== _SET_NORMAL){
				normalSet = Arrays.copyOf( items,items.length ) ;
			}
			if (setType== _SET_FACULTATIVE){
				facultativeSet = Arrays.copyOf( items,items.length ) ;
			}
			if (setType== _SET_COMPETING){
				competingSet = Arrays.copyOf( items,items.length ) ;
			}
			if (setType== _SET_EXCLUDING){
				excludingSet = Arrays.copyOf( items,items.length ) ;
			}
 
			 
		}catch(Exception e){
		}

		
	}
	
	// ------------------------------------------------------------------------



	public String getLabel() {
		return label;
	}



	public void setLabel(String label) {
		this.label = label;
	}



	public String[] getItemSet() {
		return itemSet;
	}



	public void setItemSet(String[] itemSet) {
		this.itemSet = itemSet;
	}



	public double getMissingItems() {
		return missingItems;
	}



	public void setMissingItems(double missingItems) {
		this.missingItems = missingItems;
	}



	public double getEnlargement() {
		return enlargement;
	}



	public void setEnlargement(double enlargement) {
		this.enlargement = enlargement;
	}



	public double getLengthRatio() {
		return lengthRatio;
	}



	public void setLengthRatio(double lengthRatio) {
		this.lengthRatio = lengthRatio;
	}



	public double getInsertionLength() {
		return insertionLength;
	}



	public void setInsertionLength(double insertionLength) {
		this.insertionLength = insertionLength;
	}



	public double getMinimalMatch() {
		return minimalMatch;
	}



	public void setMinimalMatch(double minimalMatch) {
		this.minimalMatch = minimalMatch;
	}



	public double[] getWeights() {
		return weights;
	}



	public void setWeights(double[] weigths) {
		this.weights = weigths;
	}



	public String[] getMandatorySet() {
		return mandatorySet;
	}



	public void setMandatorySet(String[] mandatorySet) {
		this.mandatorySet = mandatorySet;
	}



	public String[] getNormalSet() {
		return normalSet;
	}



	public void setNormalSet(String[] normalSet) {
		this.normalSet = normalSet;
	}



	public String[] getFacultativeSet() {
		return facultativeSet;
	}



	public void setFacultativeSet(String[] facultativeSet) {
		this.facultativeSet = facultativeSet;
	}



	public String[] getCompetingSet() {
		return competingSet;
	}



	public void setCompetingSet(String[] competingSet) {
		this.competingSet = competingSet;
	}



	public String[] getExcludingSet() {
		return excludingSet;
	}



	public void setExcludingSet(String[] excludingSet) {
		this.excludingSet = excludingSet;
	}



	public boolean contextIsComplete() {
		return contextComplete;
	}

	
	public boolean isContextComplete() {
		return contextComplete;
	}



	public void setContextComplete(boolean contextComplete) {
		this.contextComplete = contextComplete;
	}
	
	
	
	
}
